package com.timk.goserver.client.ui.widgets;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Class to display a link to a user's info
 * @author TKington
 *
 */
public class UserLink extends HTML {
	final PopupPanel popupToClose; // NOPMD - Explicit scoping
	final String username; // NOPMD - Explicit scoping
	final UserLinkListener listener;	// NOPMD - Explicit scoping
	
	/**
	 * Creates a UserLink
	 * @param popupToClose Optional PopupPanel to close if a user is selected
	 * @param username the username
	 * @param listener the listener for the event
	 */
	public UserLink(PopupPanel popupToClose, String username, UserLinkListener listener) {
		this.popupToClose = popupToClose;
		this.username = username;
		this.listener = listener;
		
		setText(username);
		addStyleName("goserver-UserLink");
		
		addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				if(UserLink.this.popupToClose != null) {
					UserLink.this.popupToClose.hide();
				}
				UserLink.this.listener.linkClicked(UserLink.this.username);
			}
		});
	}
}
